//
//  SHD_UserDataViewController.m
//  B段项目
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_UserDataViewController.h"

@interface SHD_UserDataViewController ()<UIScrollViewDelegate>

@end

@implementation SHD_UserDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
}
-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    _backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0 * FitHeight, 375 * FitWidth, 230 * FitHeight)];
   
    UIView *view = [[UIView alloc]initWithFrame:_backImageView.frame];
    view.backgroundColor = [UIColor blackColor];
    view.tag = 1000;
    view.alpha = .5;
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15 * FitWidth, 32 * FitHeight, 22 * FitWidth, 22 * FitHeight)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backAction)];
    [backImageView addGestureRecognizer:tap];
    backImageView.userInteractionEnabled = YES;
    backImageView.image = [UIImage imageNamed:@"返回"];
    view.userInteractionEnabled = YES;
    _backImageView.userInteractionEnabled = YES;
    
    _uesrImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50 * FitWidth, 70 * FitHeight, 40 * FitWidth, 40 * FitHeight)];
    _uesrImageView.layer.cornerRadius = 20 * FitHeight;
    [view addSubview:_uesrImageView];
    _genderImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_uesrImageView.frame.size.width + _uesrImageView.frame.origin.x + 30 * FitWidth, _uesrImageView.frame.origin.y + 12 * FitHeight , 16 * FitWidth, 16 * FitHeight)];
    [view addSubview:_genderImageView];
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_genderImageView.frame.origin.x + _genderImageView.frame.size.width + 5 * FitWidth, _genderImageView.frame.origin.y - 2 * FitHeight, 200 * FitWidth, 20 * FitHeight)];
    _titleLabel.textColor = [UIColor whiteColor];
    [view addSubview:_titleLabel];
    _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(_genderImageView.frame.origin.x, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 5 * FitHeight, 200 * FitWidth, 40 * FitHeight)];
    _contentLabel.font = [UIFont systemFontOfSize:15 * FitWidth];
    _contentLabel.textColor = [UIColor whiteColor];
    _contentLabel.numberOfLines = 0;
    [view addSubview:_contentLabel];
//-------------------------------------------------tableView
    _mytableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200 * FitHeight, 375 * FitWidth, 467 * FitHeight)];
  //  [_mytableView addSubview:_backImageView];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    [_mytableView NightWithType:UIViewColorType2];
    _mytableView.contentMode = UIViewContentModeScaleToFill;
    _mytableView.tableFooterView = [[UIView alloc]init];
    _mytableView.contentInset = UIEdgeInsetsMake(0 * FitHeight, 0, 0, 0);
    [self.view addSubview:_mytableView];
    [self.view addSubview:_backImageView];
    [self.view addSubview:view];
    [self.view addSubview:_uesrImageView];
    _uesrImageView.clipsToBounds = YES;
    [self.view addSubview:_genderImageView];
    [self.view addSubview:_titleLabel];
    [self.view addSubview:_contentLabel];
    [self.view addSubview:backImageView];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}
#pragma mark tableView的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SHD_UserDataTableCell *cell  = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SHD_UserDataTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.list = _dataArray[indexPath.row];
    return cell;
}
-(void)getData
{
    [HTTPMethod getDataByString:@"http://api2.pianke.me/profile/info" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&uid=%@&version=3.0.6", _ID]] WithDataBlock:^(id data) {
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"list"];
        NSDictionary *dataDic = [[data objectForKey:@"data"] objectForKey:@"userinfo"];
        [_backImageView sd_setImageWithURL:[NSURL URLWithString:[dataDic objectForKey:@"coverimg"]] placeholderImage:[UIImage imageNamed:@"占位"]];
        for (NSDictionary *dic in array) {
            SHD_UserDataList *list = [[SHD_UserDataList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:list];
        }
        [_uesrImageView sd_setImageWithURL:[NSURL URLWithString:[dataDic objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"占位"]];
        if ([[dataDic objectForKey:@"gender"] integerValue] == 2) {
            _genderImageView.image = [UIImage imageNamed:@"SHD_女"];
        }
        else
        {
            _genderImageView.image = [UIImage imageNamed:@"SHD_男"];
        }
        _titleLabel.text = [dataDic objectForKey:@"uname"];
        _contentLabel.text = [dataDic objectForKey:@"desc"];
        [_mytableView reloadData];
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMN_PostsViewController *xmn = [[XMN_PostsViewController alloc]init];
    xmn.idString = [_dataArray[indexPath.row] contentid];
    [self.navigationController pushViewController:xmn animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18 * FitWidth], NSFontAttributeName, nil];
    CGRect rect = [[_dataArray[indexPath.row] content] boundingRectWithSize:CGSizeMake(345 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return ceil(rect.size.height) + 100 * FitHeight;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIView *view = (UIView *)[self.view viewWithTag:1000];
    CGFloat y = scrollView.contentOffset.y;
    
    if (y < 0 * FitHeight) {
        CGRect frame  = _backImageView.frame;
        frame.origin.y = y ;
        frame.size.height = - y * 2 + 200 * FitHeight;
        frame.origin.x = y;
        frame.size.width = 375 * FitWidth - y * 2;
        _backImageView.frame = frame;
        view.frame = frame;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
